<!--
 * @Author: fisher
 * @Date: 2024-02-20 15:15:33
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-05-06 16:07:09
 * @FilePath: \allfront\src\modules\login\index.vue
 * @Description: 
-->
<template>
  <el-container class="login-container">
    <el-main class="login-main">
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        label-width="auto"
        @submit.prevent="onSubmit"
      >
        <el-form-item label="用户名" prop="username" required>
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" required>
          <el-input type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>
<script setup>
import { ref } from 'vue';
import { useAxios } from '@/hooks/useAxios';
import { useRouter } from 'vue-router';
const { post } = useAxios();
const router = useRouter();
const loginFormRef = ref(null);
const loginForm = ref({
  username: '',
  password: '',
});
const onSubmit = () => {
  const form = loginFormRef.value;
  form.validate(valid => {
    if (valid) {
      const data = {
        username: loginForm.value.username,
        password: loginForm.value.password,
      };
      post('/Auth/login', data)
        .then(res => {
          localStorage.setItem('jwtToken', res.data.token);
          router.push({
            path: '/cloud',
          });
        })
        .catch(err => {
          throw new Error('Authentication failed', err);
        });
      // TODO: Insert your authentication logic here
    } else {
      console.error('Validation failed');
      return false;
    }
  });
};
</script>
<style>
.login-container {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
}
.login-main {
  width: 100%;
  max-width: 400px;
}
</style>
